package com.huodongrili.calender;

import java.util.Calendar;

import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.huodongrili.calender.CalenderFragment.OnDaySelectChangedListener;
import com.huodongrili.calender.bg.EcOperService;
import com.huodongrili.calender.bg.LoadWeatherTask;
import com.huodongrili.calender.data.DataPreferences;

//CURL  -d  


public class MainActivity extends CalenderFragmentActivity implements
		OnDaySelectChangedListener {

	private EventsFragment mEFragment;
	private CalenderFragment mCalenderFragment;
	private GestureDetector gestureDetector;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		setPageTitleData(false);


		gestureDetector = new GestureDetector(new EnterSettingGesture(this));
		mEFragment = (EventsFragment) getSupportFragmentManager()
				.findFragmentById(R.id.events_pane);
		
		mCalenderFragment = (CalenderFragment)getSupportFragmentManager().findFragmentById(R.id.calender_pane);
		
		LinearLayout pane = (LinearLayout) findViewById(R.id.lay_title);
		pane.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Calendar  c= Calendar.getInstance();


				int month = c.get(Calendar.MONTH) + 1;
				int year = c.get(Calendar.YEAR);
				int day = c.get(Calendar.DAY_OF_MONTH);
				mCalenderFragment.backToToday(year,month);
				mEFragment.changedData(0,year, month, day);
			}
		});

		new LoadWeatherTask(getApplicationContext()).execute();


		DataPreferences dpf = DataPreferences.getInstance(getApplicationContext());
		if(!dpf.isWifiSync()){
			startService(new Intent(this,EcOperService.class));
		}
	}

	@Override
	public void onDaySelectChanged(int position,int year, int month, int day) {

		mEFragment.changedData(position,year, month, day);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int menuId = item.getItemId();
		if (menuId == R.id.menu_settings) {
			Intent i = new Intent(this, SettingActivity.class);
			startActivity(i);
		}
		return true;
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}
	

}
